package com.example.activity;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.example.activity.MainActivity;
import com.example.model.HistoryListAdapter;
import com.example.model.HistoryModel;
import com.example.model.LocationModel;
import com.example.test.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BrowserView extends Activity {
	private ImageButton search, back, forward, refresh, menu;
	private WebView webView;
	private EditText urlText;
	ListView hislist;

	private HistoryModel hisModel;
	private Cursor mCursor;
	private int accessedTimes;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browser);

		hisModel = new HistoryModel(this);
		mCursor = hisModel.select();
		hislist = (ListView) findViewById(R.id.list_his);
		hislist.setAdapter(new HistoryListAdapter(this, mCursor));

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String currentLocation = bundle.getString("CURLOC");
		TextView user = (TextView) findViewById(R.id.location_view);
		user.setText("LOC" + currentLocation);

		urlText = (EditText) findViewById(R.id.editText_url);
		 String url_q=bundle.getString("URL");
		 urlText.setText(url_q);

		search = (ImageButton) findViewById(R.id.btn_search);

		webView = (WebView) findViewById(R.id.webView1);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.setWebChromeClient(new WebChromeClient());
		webView.setWebViewClient(new WebViewClient());

		ImageButton forward = (ImageButton) findViewById(R.id.btn_forward);
		forward.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				webView.goForward();

			}

		});
		ImageButton back = (ImageButton) findViewById(R.id.btn_back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				webView.goBack();

			}

		});
		ImageButton refresh = (ImageButton) findViewById(R.id.btn_refrash);
		refresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				webView.reload();

			}

		});
		urlText.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_ENTER) {
					if (!"".equals(urlText.getText().toString())) {
						openBrowser();
						add();
						return true;
					} else {
						showDialog();
					}
				}
				return false;
			}

		});
		search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!"".equals(urlText.getText().toString())) {
					openBrowser();
					add();
				} else {
					showDialog();
				}

			}
		});

		ImageButton setbtn = (ImageButton) findViewById(R.id.btn_set);
		setbtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				openOptionsMenu();

			}

		});

	}

	private void openBrowser() {
		webView.loadUrl(urlText.getText().toString());
		Toast.makeText(this, "Waiting..." + urlText.getText().toString(),
				Toast.LENGTH_SHORT).show();
	}

	private void showDialog() {
		new AlertDialog.Builder(BrowserView.this).setTitle("Mobile Web")
				.setMessage("Pleas Input the URL")
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Log.d("WebWiew", "Press the OK Button");
					}

				}).show();
	}

	public void add() {
		String url = urlText.getText().toString();

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String location = bundle.getString("CURLOC");

		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String time = sdf.format(c.getTime());

		SimpleDateFormat tf = new SimpleDateFormat("H");
		String timef = tf.format(c.getTime());
		int t = Integer.parseInt(timef);
		String timeFrame = null;
		if (t >= 6 && t <= 8)

			timeFrame = "Morning";

		else if (t >= 9 && t < 11)
			timeFrame = "Antemeridiem";
		else if (t >= 11 && t < 13)
			timeFrame = "LunchTime";
		else if (t >= 13 && t < 17)
			timeFrame = "Afternoon";
		else if (t >= 17 && t < 19)
			timeFrame = "DinnerTime";
		else if (t >= 19 && t < 23)
			timeFrame = "Evening";
		else
			timeFrame = "Midnight";

		String id = hisModel.query_id(url, timeFrame, location);

		if (id == null) {
			accessedTimes = 1;
			hisModel.insert(url, time, timeFrame, location, accessedTimes);
			mCursor.requery();
			hislist.invalidateViews();

		} else {

			Toast.makeText(this, "old" + id, Toast.LENGTH_SHORT).show();
			int intid = Integer.parseInt(id);
			String at=hisModel.query_at(url, timeFrame, location);
			int intat=Integer.parseInt(at);
			if(intat!=0)
			{
			intat = intat + 1;
			hisModel.update(intid, url, time, timeFrame, location,
					intat);
		}}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String currentLocation = bundle.getString("CURLOC");
		Intent inthis = new Intent(BrowserView.this, HistoryView.class);
		Intent intloa = new Intent(BrowserView.this, LocationView.class);

		Intent inttop = new Intent(BrowserView.this, TopSiteView.class);
		Bundle buntop = new Bundle();

		switch (item.getItemId()) {

		case R.id.history:
			startActivity(inthis);
			return true;
		case R.id.top: {

			buntop.putString("CURLOC", currentLocation);
			inttop.putExtras(buntop);
			startActivity(inttop);
		}
			return true;
		default:
			startActivity(intloa);

		}
		return super.onOptionsItemSelected(item);
	}

}
